<?php

class AdminPage extends Admin{

	public function index(){
		$this->render("admin/page/index");
	}

	public function edit(){
		$page = $_POST["page"];
		$data = array(
			"name" => $page,
			"html" => array()
		);
		foreach ($this->langs as $key=>$label){
			$filename = "app/views/page/$key/$page.ctp";
			if(file_exists($filename)){
				$data["html"][$key] = file_get_contents($filename);
			}else{
				$data["html"][$key] = "";
			}
		}
		$this->set("page", $data);
		$this->layout = false;
		$this->render();
	}

	public function save(){
		$page = $_POST["page"];
		$name = $page["name"];
		$written = 0;
		if($page && $name){
			foreach ($page["html"] as $lang=>$html){
				$written += file_put_contents("app/views/page/$lang/$name.ctp", trim($html));
			}
		}
		ob_clean();
		header("X-Status: ".$written);
		__($written>0 ? "Strana je uspešno izmenjena" : "Strana nije izmenjena");
	}

}